﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Runtime.Remoting.Contexts;

namespace Jxym.Tools
{
    /// <summary>
    /// 浏览器请求处理
    /// 徐永明
    /// 201103
    /// http://blog.jxymnet.cn
    /// </summary>
    public class Request
    {
        /// <summary>
        /// 获取http请求参数。无参数返回字符串"0";
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Params(object keyName)
        {
            object value = HttpContext.Current.Request.Params[keyName.ToString()];

            try
            {
                if (value == null)
                    return 0;

                return value;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 前用户身份ID
        /// </summary>
        /// <returns></returns>
        public static string UserID
        {
            get
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    return HttpContext.Current.User.Identity.Name;
                }
                return "0";
            }
            set
            {
                string user = value;
                string[] users = user.Split(',');
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, users[0], DateTime.Now, DateTime.Now.AddDays(30), System.Convert.ToBoolean(users[1]), string.Empty, "/"); //建立身份验证票对象
                string HashTicket = FormsAuthentication.Encrypt(Ticket); //加密序列化验证票为字符串
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
                HttpContext.Current.Response.Cookies.Add(UserCookie); //输出Cookie
            }
        }

        /// <summary>
        /// 管理员身份ID
        /// </summary>
        /// <returns></returns>
        public static string AdminID
        {
            get
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    return HttpContext.Current.User.Identity.Name;
                }
                return "0";
            }
            set
            {
                string user = value;
                string[] users = user.Split(',');
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, users[0], DateTime.Now, DateTime.Now.AddDays(30), System.Convert.ToBoolean(users[1]), string.Empty, "/"); //建立身份验证票对象
                string HashTicket = FormsAuthentication.Encrypt(Ticket); //加密序列化验证票为字符串
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
                HttpContext.Current.Response.Cookies.Add(UserCookie); //输出Cookie
            }
        }
    }
}
